<?php 
namespace app\index\controller;
use app\index\controller\Common;
use think\Request;
use think\Db;

class News extends Common{
    public function index(){
        $new_type = Request::instance()->param('newType');
        if(empty($new_type)){
            $news = Db::name('news')->paginate(6);
        }else{
            $news = Db::name('news')->where('nid', $new_type)->paginate(6);
        }
        foreach($news as $key => $per){
            $new_arr[$key]['mid'] = $per['mid'];
            $new_arr[$key]['title'] = mb_substr($per['title'], 0, 15);
            $new_arr[$key]['content'] = $per['content'];
            $new_arr[$key]['img_url'] = $per['img_url'];
            $new_arr[$key]['pubtime'] = $per['pubtime'];
        }

        $ind_sub = Db::name('nav')->where('sid', 6)->limit(4)->select();
        $page = $news->render();
        $banner = Db::name('banner')->where('is_show', 1)->field('img_url,link_url')->limit(1)->find();
        $banner = empty($banner) ? null : $banner;

        $company = [
            'sub' => $ind_sub,
            'news' => $new_arr,
            'page' => $page,
            'banner' => $banner,
        ];
        $this->assign('news', $company);
        return $this->fetch('news');
    }

    public function newsDetail($mid){
        $banner = Db::name('banner')->where('is_show', 1)->field('img_url,link_url')->limit(1)->find();
        $banner = empty($banner) ? null : $banner;

        $news = Db::name('news')->where('mid', $mid)->field('mid,author,pubtime,content,title,img_url')->find();
        $prev = Db::name('news')->where('mid', '<', $mid)->field('mid,title')->limit(1)->find();
        $next = Db::name('news')->where('mid', '>', $mid)->field('mid,title')->limit(1)->find();

        $news['author'] = substr($news['author'],0,12);

        if($prev == null){
            $prev = '';
        }else{
            $prev['title'] = substr($prev['title'],0,12);
        }
        if($next == null){
            $next = '';
        }else{
            $next['title'] = substr($next['title'],0,12);
        }
        
        $ind_sub = Db::name('nav')->where('sid', 6)->select();
        $company = [
            'sub' => $ind_sub,
            'banner' => $banner,
            'news' => $news,
            'prev' => $prev,
            'next' => $next,
        ];
        $this->assign('detail', $company);
        return $this->fetch('new_detail');
    }
}